<template>
    <el-card class="box-card">
        <div slot="header">
            <span>添加商品</span>
        </div>
        <!-- 商品名称 -->
        <el-form :model="form" ref="form" class="demo-form-inline" :rules="rules">
            <div class="form">
                <el-form-item label="商品名称:" prop="name">
                    <el-input label="left" v-model="form.name" placeholder="商品名称"></el-input>
                </el-form-item>
            </div>

            <!-- 商品分类 -->
            <el-form-item label="商品分类:">
                <el-select v-model="form.category" placeholder="请选择商品分类">
                    <el-option v-for="(v, i) in cateList" :key="i" :label="v" :value="v"></el-option>
                </el-select>
            </el-form-item>
            <!-- 商品价格 -->
            <el-form-item label="商品价格:">
                <el-input-number v-model="form.price" :min="0"></el-input-number>
            </el-form-item>

            <!-- 商品图片 -->
            <el-form-item label="商品图片:">
                <el-upload class="avatar-uploader" action="http://api.wm.v6g.cn/goods/goods_img_upload"
                    :on-success="success" :before-upload="beforeUpload" :show-file-list="false">
                    <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <!-- 商品描述 -->
            <el-form-item label="商品描述:">
                <el-input type="textarea" v-model="form.goodsDesc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-divider></el-divider>
                <el-button type="primary" @click="add">添加商品</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
import { goodsAdd, categories } from "@/api/user.js";
export default {
    data() {
        return {
            form: {
                //名称
                name: "",
                //分类
                category: "",
                //价格
                price: "0",
                //图片
                imgUrl: "",
                // 商品描述
                goodsDesc: "",
            },
            // 商品列表分类数组
            cateList: [],
            rules: {
                title: [
                    { required: false, message: "请输入商品名称", trigger: "blur" },
                    { min: 3, message: "长度大于 3 个字以上", trigger: "blur" },
                ],
            },
        };
    },

    methods: {
        // 上传事件
        add() {
            this.$refs.form.validate(async (result) => {
                if (result) {
                    let res = await goodsAdd(this.form);
                    if (res.data.code === 0) {
                        // this.$router.push('/goods/list')
                    }
                }
            });
        },
        // 商品图片上传
        async success(res, file) {
            this.form.imgUrl = res.imgUrl;
        },
        // 图片上传
        // 上传之前执行的函数
        beforeUpload(file) {
            //MIME：文件类型的描述
            let mime = ["image/jpeg", "image/png", "image/gif"]; //允许上传三种格式的图片
            const isJPG = mime.includes(file.type);
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG、PNG、GIF 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
            }
            return isJPG && isLt2M;
        },
    },

    async created() {
        // 获取商品分类列表
        let res = await categories();
        // 吧获取到的分类，放到数组用于渲染
        this.cateList = res.data;
    },
};
</script>
<style lang="less" scoped>
.form {
    label {
        width: 90px;
    }

    width: 400px;

    div {
        display: flex;

        input {
            margin-left: 5px;
        }
    }
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

.el-textarea {
    width: 40%;
}
</style>
